<?php

class Main extends MY_Controller {
	
	private $menuItems;
	
	private $views = array(
		'publicBodyMain'	=>'public/content/publicBodyMain',
		'publicBodyPage'	=>'public/content/publicBodyPage',
		'publicBodyAbout'	=>'public/content/publicBodyAbout',
		'publicBodyContact'	=>'public/content/publicBodyContact',
		'publicBodyLogin'	=>'public/content/publicBodyLogin'
	);
	
	
	
	
	
	
	
	/**
	 * Main Constructor
	 */
	function Main() {
		parent::__construct();
		
		$this->load->helpers(array('html'));
		
		$this->load->library(array('carabiner','fuelmenu','template'));
		
		$this->load->model('Public_model');
		
		$this->lang->load('public');
		//$this->load->spark('example-spark/1.0.0');
		//$this->load->spark('datamapper/1.8.2');
		
		
		$this->_createMenuItems();	
		$this->_configTemplate();
		$this->_setData();
	}

	
	
	
	
	
	
	/**
	 * Load and config template
	 */
	private function _configTemplate() {
		$this->template->title('Radical Booking');
		$this->template->enable_parser(false);
		$this->template->set_layout('publicLayout');
		$this->template->set_partial('loginForm', 'public/partials/publicPartialLoginForm');
		$this->template->set_partial('loginInfo', 'public/partials/publicPartialLoginInfo');
	}
	
	/**
	 * Set global data to be forward to all views
	 */
	private function _setData() {
		
		$account = array();
		
		if ($this->ion_auth->logged_in()) {
			
			$account['email'] = $this->ion_auth->get_user()->email;
			
			if ($this->ion_auth->is_admin()) {
				$account['type'] = "Admin";
			}
			else if ($this->ion_auth->is_group('school')) {
				$account['type'] = "School";
			}
			else if ($this->ion_auth->is_group('hotel')) {
				$account['type'] = "Hotel";
			} 
		}
		else {
			$account = null;
		}
		
		$this->template->set('account',$account);
	}
	
	/**
	 * Create menu links
	 */
	private function _createMenuItems() {
			
		$this->menuItems =
		array (
			'Main/index'		=>	$this->lang->line('public.menu.main'),
			'Main/page'			=>	'Page',
			'Main/about'		=>	'About',
			'Main/contact'		=>	'Contact'
		);
	}
	
	/**
	 * return html with the main navigation menu
	 */
	private function _navigationMenu($active_class = 'active', $use_titles = false, $container_tag_class='nav') {
		
		$active = $this->uri->rsegment(1).$this->uri->slash_rsegment(2,'leading');
		
		$param = array(
			'active_class'			=>$active_class,
			'use_titles'			=>$use_titles,
			'container_tag_class'	=>$container_tag_class,
		);
		$this->fuelmenu->initialize($param);
		
		return $this->fuelmenu->render($this->menuItems,$active,NULL,'basic');	
	}
	
	
	/**
	 * check if someone is loged in, if so ... redirect them accordingly
	 */
	private function check_logon_users() {
	
		if ($this->ion_auth->is_admin()) {
			redirect('Admin/index');
		}
		else {
			if ($this->ion_auth->is_group($this->SchGroup)) {
				redirect('School/index');
			}
			else if($this->ion_auth->is_group($this->HotGroup)) {
				redirect('Hotel/index');
			}
		}
	}
	
	
	
	
	
	
	
	
	
	/**
	 * Entry point
	 */
	public function index() {
		
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery', 'bootstrap_cerulean'));
		else
			$this->template->set('groupLibraries',array('jquery_local','jqueryui_local','bootstrap_cerulean'));
		
		$this->template->set('menu', $this->_navigationMenu());
		
		$this->template->build($this->views['publicBodyMain']);
	}
	
	public function page() {
		
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery','bootstrap_cerulean'));
		else
			$this->template->set('groupLibraries',array('jquery_local','bootstrap_cerulean'));
		
		$this->template->set('menu', $this->_navigationMenu());
		
		$this->template->build($this->views['publicBodyPage']);
	}
	
	public function about() {
		
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery','bootstrap_cerulean'));
		else
			$this->template->set('groupLibraries',array('jquery_local','bootstrap_cerulean'));
		
		$this->template->set('menu', $this->_navigationMenu());
		
		$this->template->build($this->views['publicBodyAbout']);
	}
	
	public function contact() {
		
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery','bootstrap_cerulean'));
		else
			$this->template->set('groupLibraries',array('jquery_local','bootstrap_cerulean'));
		
		$this->template->set('menu', $this->_navigationMenu());
		
		$this->template->build($this->views['publicBodyContact']);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * Function to check the authentication, if it fails, login form (Login Controller) will be shown (called) 
	 * $_POST fields: username, password, login
	 */
	public function login() {
		
		// only enter here if there is a login request
		if($this->input->post('login')) {
			
			// set form rules
			$this->form_validation->set_rules('username','user',		'required');
			$this->form_validation->set_rules('password','password', 	'required');
				
			// define error delimiters so these can be customized with css styles
			$this->form_validation->set_error_delimiters('<p class="error">','</p>');
			
			// see if the previous rules are ok ...
			if ($this->form_validation->run()) {
				if ($_POST) {
					$user = $this->input->post('username', true);
					$pass = $this->input->post('password', true);
						
					// log in ...
					if ($this->ion_auth->login($user,$pass,false)) {
						
						if ($this->ion_auth->is_admin()) {
							redirect('Admin');
							return;
						}
						if ($this->ion_auth->is_group('school')) {
							redirect('School');
							return;
						}
						if ($this->ion_auth->is_group('hotel')) {
							redirect('Hotel');
							return;
						}
						else {
							redirect('Main/index');
						}
					}
					else {	// ... login attempt failed
						redirect("Login");
					}
				}
			}
			else {	// ... the rules dont apply? show login form again ...
				redirect("Login");
			}
		} 
			
		// ... if no redirection applied until now ... do this
		redirect("Login");
	}
	
	
	/**
	 * Logout
	 */
	public function logout() {
		
		$this->ion_auth->logout();
		redirect('Main/index');
	}
	
	
	
	
	
	
	
	
	public function tmp() {
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * old function to create all menus for the previous template
	 */
	private function _old_getMenus() {
		
		$menus = array();
		
		// render the mainMenu
		$active = $this->uri->rsegment(1).$this->uri->slash_rsegment(2,'leading'); // set the current link as active
		$menus['main'] = $this->fuelmenu->render($this->menuItems,$active,NULL,'basic');
		
		// render the footerMenu
		$active = $this->uri->rsegment(1).$this->uri->slash_rsegment(2,'leading'); // set the current link as active
		$menus['footer'] = $this->fuelmenu->render($this->menuItems,$active,NULL,'delimited');
		
		// render the loginMenu
		if ($this->ion_auth->logged_in()) {
			$menus['login'] = $this->fuelmenu->render(array('Login/index'=>$this->ion_auth->get_user()->email),null,null,'basic');
		}
		else {
			$menus['login'] = $this->fuelmenu->render(array('Login/index'=>'login'),null,null,'basic');
		}
		
		// render the languageMenu
		$language = array (
			$this->lang->switch_uri('en')=>'en',
			$this->lang->switch_uri('pt')=>'pt',
		);
		$menus['language'] = $this->fuelmenu->render($language,null,null,'basic');
		
		return $menus;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}